SetGfxDriver 1:Graphics 1024,768,8,2:SeedRnd MilliSecs() :SetBuffer FrontBuffer() 
Global in1,in2,in3,sort1,sort2,zzz1,fnt1,fnt2
in1 = CreateBank(200):in2 = CreateBank(200):in3 = CreateBank(200)
Dim decode(1000000)
Dim encode(1000000):Dim aaa(60):Dim aaa1(60):Dim aaa2(60):sort1 = CreateBank(60):sort2 = CreateBank(60):Dim zzz(66666)
Dim zzz2(8881,10)
decodet()
While Not KeyHit(1) 
ClsColor 0,0,0
Cls
randnum()
getnums()
zzz1=zzz1+1
Flip
;If zzz1> 71 Then  WaitMouse() 
VWait 
Wend 

Function mod1(x)
While x>23 
 x = x -24
Wend 
While  x<0 
 x = x +24
Wend 
Return x
End Function

Function mod2(x)
While x>255 
 x = x -256
Wend 
While  x<0 
 x = x +256
Wend 
Return x
End Function

Function mod3(x)
While x>331775
 x = x -331776
Wend 
While  x<0 
 x = x +331776
Wend 
Return x
End Function

Function mod4(x)
While x>65535
 x = x -65536
Wend 
While  x<0 
 x = x +65536
Wend 
Return x
End Function

Function fact_faza()
For unittest=1 To 24
	For i = 0 To 16 
	PokeInt in2,i*5, PeekInt (in1,i*5)
	Next 
	
;	Goto hfaza
	For hlfaza = 1 To unittest
		; ============ LOW FAZA ================

		For 	y1=0 To 16
		x = PeekInt (in2,y1*5)
;		x1 = Int(Floor(x/256))
;		x2 = x-x1*256
;		x1 =mod2(x1 -mod1(hlfaza +unittest) )
;		x2 =mod2(x2 +mod1(hlfaza +unittest))
		x =mod4(x + unittest*11111)
;		x = x1*256+x2
		 PokeInt in2,y1*5,x
		Next	
		
				; ============= 3-rd FAZA ===============
				x1 = fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10),PeekInt(in2,15))
				x2 = fact(PeekInt(in2,0+20),PeekInt(in2,5+20),PeekInt(in2,10+20),PeekInt(in2,15+20))
				x3 = fact(PeekInt(in2,0+2*20),PeekInt(in2,5+2*20),PeekInt(in2,10+2*20),PeekInt(in2,15+2*20))
				x4 = fact(PeekInt(in2,0+3*20),PeekInt(in2,5+3*20),PeekInt(in2,10+3*20),PeekInt(in2,15+3*20))
				x0 = mod3(x1 + x2*24 + x3*576 + x4*13824+ unittest*10000)
				xx4 = Int(Floor(x0/13824))
				x0 = x0  - xx4*13824
				xx3 = Int(Floor(x0/576))
				x0 = x0  - xx3*576
				xx2 = Int(Floor(x0/24))
				xx1 = x0  - xx2*24
;				DebugLog x1+" "+x2+" "+x3+" "+x4				
;				DebugLog xx1+" "+xx2+" "+xx3+" "+xx4: WaitMouse()
				If xx1 <> x1 Then
					xt1=0
					ttt1$ = decode(xx1*5)
					xa1=Mid$(ttt1$,1,1)
					xa2=Mid$(ttt1$,2,1)
					xa3=Mid$(ttt1$,3,1)
					xa4=Mid$(ttt1$,4,1)	
	        		aaa(0)=PeekInt (in2,0):aaa(5)=PeekInt (in2,5):aaa(10)=PeekInt (in2,10):aaa(15)=PeekInt (in2,15)				
					aaa2(0)=aaa(0):aaa2(5)=aaa(5):aaa2(10)=aaa(10):aaa2(15)=aaa(15)
					For x=4 To 1 Step -1
					maxx =0
					For xt=0 To 3
					xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
					Next 
					aaa(yy2) =-10:aaa1(x*5)=aaa2(yy2)
					Next							
					PokeInt in2,xt1*20 + 0*5,aaa1(xa1*5)
					PokeInt in2,xt1*20 + 1*5,aaa1(xa2*5)
					PokeInt in2,xt1*20 + 2*5,aaa1(xa3*5)
					PokeInt in2,xt1*20 + 3*5,aaa1(xa4*5)		
;				DebugLog aaa(0)+" "+aaa(5)+" "+aaa(10)+" "+aaa(15)				
;				DebugLog aaa2(0)+" "+aaa2(5)+" "+aaa2(10)+" "+aaa2(15): WaitMouse()									
				EndIf 								
				If xx2 <> x2 Then
					xt1=1
					ttt1$ = decode(xx2*5)
					xa1=Mid$(ttt1$,1,1)
					xa2=Mid$(ttt1$,2,1)
					xa3=Mid$(ttt1$,3,1)
					xa4=Mid$(ttt1$,4,1)	
	        		aaa(0)=PeekInt (in2,xt1*20 + 0):aaa(5)=PeekInt (in2,xt1*20 + 5):aaa(10)=PeekInt (in2,xt1*20 + 10):aaa(15)=PeekInt (in2,xt1*20 + 15)				
					aaa2(0)=aaa(0):aaa2(5)=aaa(5):aaa2(10)=aaa(10):aaa2(15)=aaa(15)
					For x=4 To 1 Step -1
					maxx =0
					For xt=0 To 3
					xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
					Next 
					aaa(yy2) =-10:aaa1(x*5)=aaa2(yy2)
					Next							
					PokeInt in2,xt1*20 + 0*5,aaa1(xa1*5)
					PokeInt in2,xt1*20 + 1*5,aaa1(xa2*5)
					PokeInt in2,xt1*20 + 2*5,aaa1(xa3*5)
					PokeInt in2,xt1*20 + 3*5,aaa1(xa4*5)	
							
				EndIf 	
				If xx3 <> x3 Then
					xt1=2
					ttt1$ = decode(xx3*5)
					xa1=Mid$(ttt1$,1,1)
					xa2=Mid$(ttt1$,2,1)
					xa3=Mid$(ttt1$,3,1)
					xa4=Mid$(ttt1$,4,1)	
	        		aaa(0)=PeekInt (in2,xt1*20 + 0):aaa(5)=PeekInt (in2,xt1*20 + 5):aaa(10)=PeekInt (in2,xt1*20 + 10):aaa(15)=PeekInt (in2,xt1*20 + 15)				
					aaa2(0)=aaa(0):aaa2(5)=aaa(5):aaa2(10)=aaa(10):aaa2(15)=aaa(15)
					For x=4 To 1 Step -1
					maxx =0
					For xt=0 To 3
					xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
					Next 
					aaa(yy2) =-10:aaa1(x*5)=aaa2(yy2)
					Next							
					PokeInt in2,xt1*20 + 0*5,aaa1(xa1*5)
					PokeInt in2,xt1*20 + 1*5,aaa1(xa2*5)
					PokeInt in2,xt1*20 + 2*5,aaa1(xa3*5)
					PokeInt in2,xt1*20 + 3*5,aaa1(xa4*5)					
				EndIf 	
				If xx4 <> x4 Then
					xt1=3
					ttt1$ = decode(xx4*5)
					xa1=Mid$(ttt1$,1,1)
					xa2=Mid$(ttt1$,2,1)
					xa3=Mid$(ttt1$,3,1)
					xa4=Mid$(ttt1$,4,1)	
	        		aaa(0)=PeekInt (in2,xt1*20 + 0):aaa(5)=PeekInt (in2,xt1*20 + 5):aaa(10)=PeekInt (in2,xt1*20 + 10):aaa(15)=PeekInt (in2,xt1*20 + 15)				
					aaa2(0)=aaa(0):aaa2(5)=aaa(5):aaa2(10)=aaa(10):aaa2(15)=aaa(15)	
					For x=4 To 1 Step -1
					maxx =0
					For xt=0 To 3
					xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
					Next 
					aaa(yy2) =-10:aaa1(x*5)=aaa2(yy2)
					Next							
					PokeInt in2,xt1*20 + 0*5,aaa1(xa1*5)
					PokeInt in2,xt1*20 + 1*5,aaa1(xa2*5)
					PokeInt in2,xt1*20 + 2*5,aaa1(xa3*5)
					PokeInt in2,xt1*20 + 3*5,aaa1(xa4*5)					
				EndIf
								

		; ============= HIGH FAZA ===============			
		For xt1=0 To 3
			For xt=0 To 3
				PokeInt sort1,xt*5,PeekInt (in2,xt1*20 + xt*5)	
				PokeInt sort2,xt*5,PeekInt (in2,xt1*20 + xt*5)					
			Next		
				For xt=0 To 3
					aaa(xt*5)=PeekInt (sort1,xt*5)+1
				Next
				For x=4 To 1 Step -1
				maxx =0
				For xt=0 To 3
				xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
				Next 
				aaa(yy2) =-10:aaa1(x*5)=PeekInt (sort2,yy2):PokeInt sort1,yy2,x
				Next
				ttt = Int(""+ PeekInt (sort1,0*5) +""+PeekInt (sort1,1*5) +""+PeekInt (sort1,2*5)+""+PeekInt (sort1,3*5))
				xx = encode(ttt*5): 
;				DebugLog PeekInt (sort2,0)+" "+PeekInt (sort2,5)+" "+PeekInt (sort2,10)+" " + PeekInt (sort2,15) 
;				DebugLog xx		
				xx = mod1(xx - unittest) 
;				DebugLog xx						
				ttt1$ = decode(xx*5)
				x1=Mid$(ttt1$,1,1)
				x2=Mid$(ttt1$,2,1)
				x3=Mid$(ttt1$,3,1)
				x4=Mid$(ttt1$,4,1)
;				DebugLog aaa1(5)+" "+aaa1(10)+" "+aaa1(15)+" " + aaa1(20)
;				DebugLog aaa1(x1*5)+" "+aaa1(x2*5)+" "+aaa1(x3*5)+" " + aaa1(x4*5): WaitMouse ()
				PokeInt in2,xt1*20 + 0*5,aaa1(x1*5)
				PokeInt in2,xt1*20 + 1*5,aaa1(x2*5)
				PokeInt in2,xt1*20 + 2*5,aaa1(x3*5)
				PokeInt in2,xt1*20 + 3*5,aaa1(x4*5)	
		Next	
	Next	
.hfaza	
		
 				

	eee =fact(fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10),PeekInt(in2,15)),fact(PeekInt(in2,0+20),PeekInt(in2,5+20),PeekInt(in2,10+20),PeekInt(in2,15+20)),fact(PeekInt(in2,0+2*20),PeekInt(in2,5+2*20),PeekInt(in2,10+2*20),PeekInt(in2,15+2*20)),fact(PeekInt(in2,0+3*20),PeekInt(in2,5+3*20),PeekInt(in2,10+3*20),PeekInt(in2,15+3*20)))
If eee = unittest Then zzz(zzz1)=zzz(zzz1)+1:zzz2(zzz1,zzz2(zzz1,10))=unittest:zzz2(zzz1,10)=zzz2(zzz1,10)+1
Next

End Function

Function fact(x1,x2,x3,x4)
        aaa(0)=x1:aaa(5)=x2:aaa(10)=x3:aaa(15)=x4
	For x=4 To 1 Step -1
	maxx =0
	For xt=0 To 3
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =-10:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5))
	xx = encode(ttt*5)
	Return xx
End Function

Function getnums()
	For y=0 To 3
	For x=0 To 3
	Color 0,255,0:Text 30+x*30,y*9+10,PeekInt(in1,5*(y*4+x)),False ,False 
	Next
	Next
	xx1=fact(PeekInt(in1,0),PeekInt(in1,5),PeekInt(in1,10),PeekInt(in1,15)):Text 150,10,xx1,False ,False 
	xx2=fact(PeekInt(in1,0+20),PeekInt(in1,5+20),PeekInt(in1,10+20),PeekInt(in1,15+20)):Text 150,19,xx2,False ,False 
	xx3=fact(PeekInt(in1,0+2*20),PeekInt(in1,5+2*20),PeekInt(in1,10+2*20),PeekInt(in1,15+2*20)):Text 150,28,xx3,False ,False 
	xx4=fact(PeekInt(in1,0+3*20),PeekInt(in1,5+3*20),PeekInt(in1,10+3*20),PeekInt(in1,15+3*20)):Text 150,37,xx4,False ,False 
	Text 170,22,fact(xx1,xx2,xx3,xx4),False ,False 
	
	fact_faza()
	
	For y=0 To 3
	For x=0 To 3
	Color 255,255,0:Text 30+x*30,y*9+50,PeekInt(in2,5*(y*4+x)),False ,False 
	Next
	Next
	xx1=fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10),PeekInt(in2,15))
	xx2=fact(PeekInt(in2,0+20),PeekInt(in2,5+20),PeekInt(in2,10+20),PeekInt(in2,15+20))
	xx3=fact(PeekInt(in2,0+2*20),PeekInt(in2,5+2*20),PeekInt(in2,10+2*20),PeekInt(in2,15+2*20))
	xx4=fact(PeekInt(in2,0+3*20),PeekInt(in2,5+3*20),PeekInt(in2,10+3*20),PeekInt(in2,15+3*20))
	Text 170,62,fact(xx1,xx2,xx3,xx4),False ,False 
	ttt0=0
	For i=0 To zzz1
	;If zzz(i) = 0 Then ttt0 = ttt0+1
	;If zzz(i) = 0 Then ttt0 = ttt0+1
	ttt0 = ttt0+zzz(i)
	Color 255,255,0:Text (i*12) Mod 840,130+68*Int(i/70),Str(zzz(i))+",",False ,False 
	For i9=0 To zzz2(i,10)
	Color 0,0,255:Text (i*12) Mod 840,130+68*Int(i/70)+i9*10+10,Str(zzz2(i,i9))+"",False ,False 	
	Next	
    Next
;	ttt0 = Ceil(ttt0 * 100 /  (zzz1+1))
	ttt0 = Ceil(ttt0 /  (zzz1+1))	
;	SetFont fnt2
	Color 255,255,0:Text 10,100," " + ttt0 + " ",False ,False
;	SetFont fnt1 
End Function


Function randnum()
For i = 0 To 16 
t = Rand(0,65535):PokeInt in1,i*5, t:PokeInt in2,i*5, t
Next 
End Function

Function decodet()
y=0
For x1=1 To 4
For x2=1 To 4
For x3=1 To 4
For x4=1 To 4
If((x1 <> x2) And (x1<>x3) And (x1<>x4) And (x2<>x3) And (x2<>x4) And (x3<>x4)) decode(y*5) =x1+""+x2+""+x3+""+x4:encode(Int(x1+""+x2+""+x3+""+x4)*5)=y:y=y+1
Next
Next
Next
Next
fnt1=LoadFont("Tahoma" ,9,False,False,False) 
fnt2=LoadFont("Arial",25,True,False,False) 


End Function